package compilator4urbi.actions;

import java.io.File;
import java.io.IOException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.jdom.JDOMException;

import tom.library.sl.VisitFailure;

import compilator4urbi.Activator;
import compilator4urbi.actions.generate.MissionBuilder;
import compilator4urbi.actions.mapping.InvalidTargetException;
import compilator4urbi.actions.mapping.Mapping;
import compilator4urbi.compilator.Compilator;
import compilator4urbi.compilator.CompilatorWifibot;
import compilator4urbi.exceptions.InvalidFSMException;


public class GenerateUrbiCode implements IObjectActionDelegate {

	IFile file;

	public GenerateUrbiCode() {}

	@Override
	public void run(IAction action) {
		if (file == null || !file.exists())
			return;
		NullProgressMonitor monitor = new NullProgressMonitor();
		monitor.beginTask(Activator.getDefault().getResourceBundle().getString("Generating"), 2);

		File f = new File (file.getLocation().toOSString());
		Activator activator = Activator.getDefault();
		try {
			int target = Mapping.resolveTarget(new File(file.getLocation().toOSString() + "map"));
			
			MissionBuilder builder = new MissionBuilder(file);
			builder.build();
			monitor.worked(1);
			switch (target) {
				case Mapping.TARGET_EPUCK:
					Compilator.generateUrbiCode(builder.getMission(), f, false);
					break;
				case Mapping.TARGET_WIFIBOT:
					CompilatorWifibot.generateUrbiCode(builder.getMission(), f);
					break;
			}
			monitor.worked(2);
			Activator.getDefault().showLocalizedMessage("UrbiGenerationDone");
		} catch (InvalidTargetException e) {
			activator.showMessage(activator.getResourceBundle().getString("InvalidTarget") + 
					" : " + e.getMessage());
		} catch (JDOMException e) {
			activator.showLocalizedMessage("InvalidMapping");
		} catch (IOException e) {
			activator.showLocalizedMessage("MappingNotFound");
		} catch (InvalidFSMException e) {
			activator.showMessage(activator.getResourceBundle().getString("Error : ") +
					" : " + e.getMessage());
		} catch (VisitFailure e) {
			activator.showMessage(activator.getResourceBundle().getString("Error : ") +
					" : " + e.getMessage());
		} finally {
			monitor.done();
		}
		try {
			file.getParent().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
		} catch (CoreException e) {}
	}



	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection == null){
			return;
		}
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection sel = (IStructuredSelection)selection;
			Object obj = sel.getFirstElement();
			if (obj instanceof IFile) {
				file = (IFile)obj;
			}
		}
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {}

}
